﻿using System;
using System.Net.Http;
using AutoMapper;
using Common.Commands.LeadCommands;
using Common.Service;
using Dto.ApiRequests.LeadForms;
using NSBus.Dto.Commands;
using NServiceBus;
using WebApp.Services;
using Sanelib.Dto;

namespace WebApp.ControllerApis
{
    public class AnonymousLeadController : SmartApiController
    {
        private readonly IModelService _modelService;
        public IBus Bus { get; set; }

        public AnonymousLeadController(IUserSession userSession, IMappingEngine mappingEngine, IModelService modelService)
            : base(userSession, mappingEngine)
        {
            _modelService = modelService;
        }

        public HttpResponseMessage Post(AddAnonymousLeadForm form)
        {            
            var errorResult = new ErrorResult();
            var isError = _modelService.CheckAnonymousLeadForm(form, errorResult);

            if (!isError)
                return Content(errorResult);

            Bus.Send<AddLeadCommand>(x =>
            {
                x.Name = form.Name;
                x.Email = form.Email;
                x.Phone = form.Phone;
                x.CompanyName = form.CompanyName;
                x.Description = form.Description;
                x.CreatedOn = DateTime.Now;
            });

            var response = new WebApiResultResponse
            {
                IsSuccess = true,
                Message = "Your request has been received."
            };

            return Content(response);

        }
    }
}
